package org.skipper.ai.strategy;

import org.skipper.ai.enums.AiTypeEnum;
import org.skipper.ai.param.SimpleChatParam;
import org.skipper.ai.strategy.domain.ChatParam;
import org.skipper.ai.strategy.domain.aliyun.AliyunChatParam;
import org.skipper.ai.strategy.domain.aliyun.AliyunMessage;
import org.skipper.ai.strategy.impl.AliyunDashScopeChat;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * Description: TODO
 *
 * @author shoujian
 * Date: 2025-10-09
 * Time: 16:05
 */
public class ChatContext {

    private final Map<AiTypeEnum, ChatStrategy> chatStrategyMap = new EnumMap<>(AiTypeEnum.class);

    public ChatContext() {
        chatStrategyMap.put(AiTypeEnum.ALI_TONGYI, new AliyunDashScopeChat());
    }

    /**
     * 获取实例
     *
     * @param aiTypeEnum ai型枚举
     * @return {@link ChatStrategy }
     */
    public ChatStrategy getInstance(AiTypeEnum aiTypeEnum) {
        ChatStrategy chatStrategy = chatStrategyMap.get(aiTypeEnum);
        if (AiTypeEnum.ALI_TONGYI.equals(aiTypeEnum)) {
            return chatStrategy;
        }
        return null;
    }

    /**
     * 构建聊天入参
     *
     * @param simpleChatParam 简单聊天入参
     * @return {@link ChatParam }
     */
    public ChatParam buildChatParam(SimpleChatParam simpleChatParam) {
        AiTypeEnum aiTypeEnum = AiTypeEnum.getByCode(simpleChatParam.getAiType());
        switch (aiTypeEnum) {
            case ALI_TONGYI:
                AliyunChatParam aliyunChatParam = new AliyunChatParam();
                aliyunChatParam.setEnableThinking(simpleChatParam.getEnableThinking());
                List<AliyunMessage> aliyunMessages = simpleChatParam.getMessages().stream()
                        .map(message -> new AliyunMessage(message.getContent(), message.getRole()))
                        .collect(Collectors.toList());
                aliyunChatParam.setAliyunMessages(aliyunMessages);
                aliyunChatParam.setModel(simpleChatParam.getModel());
                return aliyunChatParam;
            default:
                throw new IllegalArgumentException("未知类型: " + simpleChatParam.getAiType());
        }

    }
}
